#region MIT License

/*
 *  Copyright (c) 2010 Nathan Palmer
 *  http://www.nathanpalmer.com/
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

#endregion

using System;
using System.Collections.Generic;
using Quartz;
using Rhino.ServiceBus;
using SimpleScheduler.Core.Common;
using SimpleScheduler.Core.Domain;
using Trigger = Quartz.Trigger;

namespace SimpleScheduler.Server.Listener
{
    public class JobChangeNotifier : ISchedulerListener
    {
        private readonly IServiceBus bus;
        private IScheduler scheduler;
        private Dictionary<string, string> jobDictionary;

        public JobChangeNotifier(IScheduler Scheduler, IServiceBus ServiceBus)
        {
            scheduler = Scheduler;
            bus = ServiceBus;

            jobDictionary = new Dictionary<string, string>();
        }

        public void JobScheduled(Trigger trigger)
        {
            var jobDetail = scheduler.GetJobDetail(trigger.JobName, trigger.JobGroup);

            var jobCreated = new JobCreated
                                 {
                                     JobName = jobDetail.Name,
                                     GroupName = jobDetail.Group
                                 };

            var triggerFullName = string.Format("{0}/{1}", trigger.Group, trigger.Name);
            var jobFullName = JobHelpers.GenerateJobKey(jobDetail.Name, jobDetail.Group);

            if (!jobDictionary.ContainsKey(triggerFullName))
                jobDictionary.Add(triggerFullName, jobFullName);

            if (bus != null) bus.Notify(jobCreated);
        }

        public void JobUnscheduled(string triggerName, string triggerGroup)
        {
            var triggerFullName = string.Format("{0}/{1}", triggerGroup, triggerName);
            if (jobDictionary.ContainsKey(triggerFullName))
            {
                var jobFullName = jobDictionary[triggerFullName];
                var jobFullNameSplit = jobFullName.Split('/');

                var jobName = jobFullNameSplit[1];
                var groupName = jobFullNameSplit[0];

                var jobRemoved = new JobRemoved()
                                     {
                                         JobName = jobName,
                                         GroupName = groupName
                                     };

                if (bus != null) bus.Notify(jobRemoved);

                jobDictionary.Remove(triggerFullName);
            }
        }

        public void TriggerFinalized(Trigger trigger)
        {
            
        }

        public void TriggersPaused(string triggerName, string triggerGroup)
        {
            
        }

        public void TriggersResumed(string triggerName, string triggerGroup)
        {
            
        }

        public void JobAdded(JobDetail jobDetail)
        {
            
        }

        public void JobDeleted(string jobName, string groupName)
        {
            
        }

        public void JobsPaused(string jobName, string jobGroup)
        {
            
        }

        public void JobsResumed(string jobName, string jobGroup)
        {
            
        }

        public void SchedulerError(string msg, SchedulerException cause)
        {
            
        }

        public void SchedulerInStandbyMode()
        {
            
        }

        public void SchedulerStarted()
        {
            
        }

        public void SchedulerShutdown()
        {
            
        }

        public void SchedulerShuttingdown()
        {
           
        }
    }
}